<?php namespace App\Http\Controllers\user\uc\ass;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use Illuminate\Http\Request;
use App\Models\EnterpricePowerusersBase;
use App\Models\News;
use Input;
use Session;
use Validator;
use Illuminate\Pagination\LengthAwarePaginator;
use Landish\Pagination\Pagination as BootstrapPresenter;


class NewsController extends Controller {

	/**
	 * Display a listing of the resource.
	 *
	 * @return Response
	 */
	public function index()
	{
		$uid = $this->getUid();
		$model = News::where("uid", $uid);
		if($model->count()){
			$numPerPage = 20;
			$count = $model->count();

			$curPage = (int)Input::get("page");
			if(!$curPage){
				$curPage = 1;
			}
			$p = new LengthAwarePaginator($model, $count, $numPerPage, $curPage);
			$p->setPath("");
	        $presenter = new BootstrapPresenter($p);
	        $data = $model->skip(($curPage - 1) * $numPerPage)->take($numPerPage)->get()->toArray();

		}else{
			$data = array();
			$presenter = '';
		}
		return view('user.uc.ass.news', ['data'=>$data])->withPresenter($presenter)->withIsAssNews(true);
	}

	public function saveNews()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$uid = $this->getUid();
			$data = Input::all();
			$imgArr = Input::only("image_name");
			$path = base_path() . "/public/upload/";
			$dir = base_path() . "/public/images/" . $uid ."/" . env("OTHERS_DIR");
			is_dir($dir) || mkdir($dir, 0777, true);
			foreach ($imgArr as $key => $value) {
				$temp = $path . $value;
				$newPath = $dir . '/' . $value;
				if(is_file($temp)){
					rename($temp, $newPath);
				}
			}
			$data['uid'] = $uid;
			News::create($data);
			return $this->json([]);
		}else{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function updateNews()
	{
		$rule = [];
		$msg = [];
		$validator = Validator::make(Input::all(), $rule, $msg);
		if($validator->passes()){
			$nid = Input::get("nid");
			$uid = $this->getUid();
			$model = News::where("id", $nid);
			if($model->count() && $model->get()[0]->uid = $uid){
				$data = Input::except("nid");
				$imgArr = Input::only("image_name");
				$path = base_path() . "/public/upload/";
				$dir = base_path() . "/public/images/" . $uid ."/" . env("OTHERS_DIR");
				is_dir($dir) || mkdir($dir, 0777, true);
				foreach ($imgArr as $key => $value) {
					$temp = $path . $value;
					$newPath = $dir . '/' . $value;
					if(is_file($temp)){
						rename($temp, $newPath);
					}
				}
				if($model->update($data)){
					return $this->json([]);
				}
			}else{
				return App::abort(404);
			}


		}else{
			$errors = $validator->messages();
			return self::error('',$errors->first());
		}
	}

	public function editNews()
	{
		$nid = Input::get("nid", 0);
		$uid = $this->getUid();
		$model = News::where("id", $nid);
		if($model->count() && $model->get()[0]->uid == $uid){
			$data = $model->get()[0]->toArray();
		}else{
			return App::abort(404);
		}
		return view("user.uc.ass.edit_news", ['data' => $data])->withIsAssNews(true);
	}

	public function addNews()
	{
		return view("user.uc.ass.add_news")->withIsAssNews(true);
	}

	public function newsDetail()
	{
		return view("user.uc.ass.news_detail")->withIsAssNews(true);
	}



	/**
	 * Show the form for creating a new resource.
	 *
	 * @return Response
	 */
	public function create()
	{
		//
	}

	/**
	 * Store a newly created resource in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		//
	}

	/**
	 * Display the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}

	/**
	 * Show the form for editing the specified resource.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		//
	}

	/**
	 * Update the specified resource in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		//
	}

	/**
	 * Remove the specified resource from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		//
	}
}
